package net.wanglu.www.zzz.service.se.obj;

import java.util.HashMap;
import java.util.regex.Pattern;

import net.wanglu.www.zzz.service._Service;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeGraphNode;
import edu.stanford.nlp.trees.TypedDependency;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;

public class IObjExtracter extends _Service {

	@Override
	public Object service(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		TypedDependency iobj = (TypedDependency) objects[1];

		Tree root = (Tree) preprocessData.get("root");
		
		final TreeGraphNode fobject = iobj.dep();
		final TreeGraphNode fobjectGov = iobj.gov();
		final String frelation = iobj.reln().getShortName();

		Tree objAncestor = null;
		sb.clear();
		sb.append("/\\w+/").append("$").append("(");
		sb.append("/").append(Pattern.quote(fobjectGov.label().tag())).append("/");
		sb.append("<");
		sb.append("/").append(Pattern.quote(fobjectGov.label().word())).append("/");
		sb.append(")");
		sb.append("<<").append("(");
		sb.append("/").append(Pattern.quote(fobject.label().tag())).append("/");
		sb.append("<");
		sb.append("/").append(Pattern.quote(fobject.label().word())).append("/");
		sb.append(")");
		TregexPattern pattern = TregexPattern.compile(sb.toString());
		TregexMatcher matcher = pattern.matcher(root);
		if ( matcher.find()) {
			objAncestor = matcher.getMatch();
		}				
		final Tree fobjectAncestor = objAncestor;
		
		return new _ObjectTuple() {
			{
				this.setObject(fobject);
				this.setObjectGov(fobjectGov);
				this.setRelation(frelation);
				this.setObjectAncestor(fobjectAncestor);
			}
			@Override
			public Object customize() throws Exception {
				// TODO Auto-generated method stub
				return null;
			}
		};
	}
}
